#include "../seqlist.h"

typedef struct
{
    datatype key; // 关键字
    int address;
} indexnode;

int IndexSeqSearch(seqlist l, indexnode index[], int m, datatype key) {
    int i = 0, j, last;
    while( i < m && key > index[i].key) i++;  // 顺序查找
    if(i >= m) return -1;  // 查找失败
    else {
        if( i == m - 1) {
            j = l.len - 1;
        } else {
            j = index[i + 1].address - 1;
        }

        while (j >= index[i].address && l.data[j] != key)
        {
            j--;
        }

        if(j >= index[i].address) return j;  // 查找成功
        else return -1;  // 查找失败
        
    }
}